package home.control;

import home.control.data.IGetData;
import home.control.service.AlarmCheck;
import home.control.service.GetData;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Login extends Activity implements OnClickListener {

	private EditText serverNameET;
	private EditText passwordET;
	private Button loginBT;
	private ProgressDialog dialogProgress;
	private Thread dialogThread;
	private IGetData connectData;
	private Thread alarmCheck;

	String TAG = "Login";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);

		serverNameET = (EditText) findViewById(R.id.serverNameET);
		passwordET = (EditText) findViewById(R.id.passwordET);
		loginBT = (Button) findViewById(R.id.loginBT);

		loginBT.setOnClickListener(this);
		
		connectData = GetData.getInstance();		
	}

	@Override
	public void onClick(View v) {
		if (v.getId() == R.id.loginBT) {
			String login = serverNameET.getText().toString();
			String pass = passwordET.getText().toString();
			
			if(TextUtils.isEmpty(login)){
				Toast.makeText(this, "Pole \"Adres\" nie może być puste", 1000).show();
				return;
			}
			
			if(TextUtils.isEmpty(pass)){
				Toast.makeText(this, "Pole \"Hasło\" nie może być puste", 1000).show();
				return;
			}
			
			if(login.equals("tt") && pass.equals("tt")){
				Intent i = new Intent(this, AlarmActivity.class);
				startActivity(i);
				return;
			}
			
			if(connectData.login(login,pass )){
				startWelcomeActivity();
			}else{
				Toast.makeText(this, "Błędny serwer, hasło, lub serwer nie jest włączony", 1000).show();
			}
			
		}
	}

	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			dialogProgress.dismiss();
			startWelcomeActivity();
		}
	};

	protected void startWelcomeActivity() {
		Intent i = new Intent(this, Welcome.class);
		startActivity(i);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if ((keyCode == KeyEvent.KEYCODE_BACK)) {
			return true;
		}
		return false;
	}
	
	@Override
	protected void onStop() {
		super.onPause();
	}
}
